<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/17
 * Time: 14:35
 */
namespace app\mtsuperadmin\controller;

use app\api\model\MtUserVip;
use app\api\model\MtVipClass;
use app\util\ReturnCode;
use app\command\controller\Common;

class AdminSysVipManage extends Base{
    /**
     * vip卡的修改添加
     * @return array
     */
    public function adminVipClass(){
        $rule = [
            "vipName" => "require",
            "vipPrice" => "require|number",
            "cardContent" => "require",
            "discount" => "require|number",
        ];
        $msg = [];
        $vipClassData = $this->request->post();
        $result = (new Common())->VerifiString($rule,$msg,$vipClassData);
        if($result['code'] == 2){
            return $this->bindError(ReturnCode::PARAM_INVALID,$result['msg']);
        }
        if($vipClassData['vipLevel']){
            $res = (new MtVipClass())->update($vipClassData);
        }else{
            $res = (new MtVipClass())->insert($vipClassData,true);
        }
        if(!$res){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"修改失败");
        }
        return $this->bindSuccess("修改成功");
    }

    /**
     * vip卡分类列表
     * @return array
     */
    public function vipClassList(){
        $list = (new MtVipClass())->select();
        if(!$list){
            return $this->bindError(ReturnCode::DB_READ_ERROR,"查询失败");
        }
        return $this->bindSuccess("查询成功",$list);
    }

    /**
     * 用户VIP列表
     * @return array
     */
    public function userVipList(){
        $p = intval($this->request->get("p")) ? intval($this->request->get("p")) : 1;
        $limit = intval($this->request->get("limit")) ? intval($this->request->get("limit")) : config("apiadmin.ADMIN_LIST_DEFAULT");
        $list = (new MtUserVip())->page($p,$limit)->select();
        if(!$list){
            return $this->bindError(ReturnCode::DB_READ_ERROR,"查询失败");
        }
        return $this->bindSuccess("产寻成功",$list);
    }

    /**
     * 绑定用户为vip
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function operaUserVip(){
        $rule = [
            "userID" => "require|number",
            "vipLevel" => "require|number",
        ];
        $msg = [];
        $vipClassData = $this->request->post();
        $result = (new Common())->VerifiString($rule,$msg,$vipClassData);
        if($result['code'] == 2){
            return $this->bindError(ReturnCode::PARAM_INVALID,$result['msg']);
        }
        $vip = (new MtUserVip())->where("vipLevel",$vipClassData['vipLevel'])->find();
        if(!$vip){
            return $this->bindError(ReturnCode::DB_READ_ERROR,"vip分类查询失败");
        }
        $vipClassData['vipStart'] = intval($vipClassData['vipStart']) ? intval($vipClassData['vipStart']) : time();
        $vipClassData['vipEnd'] = intval($vipClassData['vipStart']) + intval($vipClassData['lengthOfTime']);
        $vipClassData['lengthOfTime'] = intval($vipClassData['lengthOfTime']) ? intval($vipClassData['lengthOfTime']) : 31536000;
        $res = (new MtUserVip())->insert($vipClassData,true);
        if(!$res){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"入库失败");
        }
        return $this->bindSuccess("入库成功");
    }
}